unit smClassGame;

interface

uses
  SysUtils, Lua, smExceptions;

const
  luaCLASS_GAME: AnsiString = 'Game';

  procedure OpenClass(const Lua: lua_State);

implementation

uses
  smMain, smSceneManager, smLuaUtils, Gen2;

function _GameSetScene(L: lua_State): Integer; cdecl; forward;
function _GameLog(L: lua_State): Integer; cdecl; forward;
function _GameQuit(L: lua_State): Integer; cdecl; forward;

const
  GAME_FUNCS: array [0..3] of luaL_reg = (
    (name: 'SetScene'; func: _GameSetScene),
    (name: 'Log'; func: _GameLog),
    (name: 'Quit'; func: _GameQuit),
    (name: nil; func: nil)
  );



function _GameSetScene(L: lua_State): Integer; cdecl;
begin
  try
    if lua_isstring(L, 1) then
      SceneManager.SetNextScene(lua_tostring(L, 1))
    else
      raise ELuaArgumentCheck.Create(L, 'not a valid scene name');
  except
    // do nothing...
  end;

  Result := 0;
end;

function _GameLog(L: lua_State): Integer; cdecl;
begin
  try
    if lua_isstring(L, 1) then
      G2WriteLogTimed(lua_tostring(L, 1), 'Script log')
    else
      raise ELuaArgumentCheck.Create(L, 'not a valid log message');
  except
    // do nothing...
  end;

  Result := 0;
end;

function _GameQuit(L: lua_State): Integer; cdecl;
begin
  if SceneManager.CurrentScene.State = ssReady then
    SceneManager.UnloadScene(True, True);

  Game.Running := False;
  Result := 0;
end;

procedure OpenClass;
begin
  // Register class "Game" and corresponding functions
  SetClass(luaCLASS_GAME, @GAME_FUNCS[0]);

end;

end.
